home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / AlexNeXTSTEPSource / Source / Chapter10_Help / Words / TextController.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  150 lines

  1. #import <appkit/appkit.h>
  2. #import "Document.h"
  3. #import "TextController.h"
  4.  
  5. # define FILE_EXTENSION "word"
  6.  
  7. // this class is responsible for managing
  8. // the savepanel, openpanel and files opened
  9. // from the Workspace
  10. @implementation TextController
  11.  
  12. // automatically sent after the .nib file
  13. // has finished loading
  14. - awakeFromNib
  15. {
  16.     // create the savepanel and openpanel
  17.     savePanel = [SavePanel new];
  18.     openPanel = [OpenPanel new];
  19.     // by default, an openpanel only allows
  20.     // one file to be opened
  21.     [openPanel allowMultipleFiles:YES];
  22.     return self;
  23. }
  24.  
  25. // create a new document in response
  26. // to the New menu option
  27. - newDocument:sender
  28. {
  29.     id document = [[Document alloc] init];
  30.     [document showDocument];
  31.     return self;
  32. }
  33.  
  34. - (BOOL)appAcceptsAnotherFile:sender
  35. {
  36.     // Double-clicking on a file in the WSM
  37.     // causes the following actions:
  38.     // * NeXTSTEP sends an openFile:ok
  39.     //     message to NXApp
  40.     // * openFile:ok sends a appAcceptsAnotherFile:
  41.     //    message to check if it's OK to open
  42.     //    a file; by returning YES, the app
  43.     //    allows files to be opened from the WSM
  44.     // * openFile:ok then sends a
  45.     //    app:openFile:type message to actually
  46.     //    open the file; the app:openFile:type
  47.     //    should return YES if successful and
  48.     //    NO if not
  49.     // the printf statements illustrate the orders
  50.     // these messages are being sent
  51.     printf("in appAcceptsAnotherFile:\n");
  52.     return YES;
  53. }
  54.  
  55. // actually open the file here
  56. - (int)app:sender openFile:(const char*)filename
  57.     type:(const char *)filetype
  58. {
  59.     printf("In app:openFile:type:\n");
  60.     return ([[Document alloc]
  61.         initDocumentFromFile:filename]);
  62. }
  63.  
  64. // open the document in response to
  65. // the Open menu option
  66. - showOpenPanel:sender
  67. {
  68.     const char *file, *directory;
  69.     const char *const *filenames;
  70.     static const char
  71.         *const wordTypes[] = {FILE_EXTENSION, NULL};
  72.     char fullPathName[81];
  73.  
  74.     // display only files with "word" extension
  75.     if ([openPanel runModalForTypes:wordTypes])
  76.         {
  77.         // get list of filename(s) selected
  78.         // the filenames method returns a
  79.         // a pointer to all the strings
  80.         filenames = [openPanel filenames];
  81.         do
  82.             {
  83.             // get directory first
  84.             directory = [openPanel directory];
  85.             // get filename
  86.             file = *(filenames++);
  87.             // construct entire pathname
  88.             strcpy(fullPathName, directory);
  89.             // append directory to filename
  90.             strcat(fullPathName, "/");
  91.             strcat(fullPathName, file);
  92.             [[Document alloc]
  93.                 initDocumentFromFile:fullPathName];
  94.             }
  95.         while (*(filenames) != NULL);
  96.             }
  97.     return self;
  98. }
  99.  
  100. // allow the document to be saved under
  101. // a different name
  102. - saveAs:sender
  103. {
  104.     const char *wordType = FILE_EXTENSION;
  105.     const char *fullPathName;
  106.     id document = [[NXApp mainWindow] delegate];
  107.     const char *filename = "";
  108.  
  109.     [savePanel setRequiredFileType:wordType];
  110.     if ([savePanel runModal])
  111.         {
  112.         fullPathName = [savePanel filename];
  113.         [document saveDocumentToFile:fullPathName];
  114.         }
  115.     return self;
  116. }
  117.  
  118. // save the document in response to
  119. // the Save menu option
  120. - showSavePanel:sender
  121. {
  122.     const char *wordType = FILE_EXTENSION;
  123.     const char *fullPathName;
  124.     id document = [[NXApp mainWindow] delegate];
  125.     const char *filename = [document filename];
  126.  
  127.     // if document has not been saved
  128.     // then present savepanel to get filename
  129.     if (!filename)
  130.         {
  131.         [savePanel setRequiredFileType:wordType];
  132.         if ([savePanel runModal])
  133.             {
  134.             fullPathName = [savePanel filename];
  135.             [document saveDocumentToFile:fullPathName];
  136.             }
  137.         }
  138.     // else just save the file
  139.     else
  140.         [document saveDocumentToFile:filename];
  141.     return self;
  142. }
  143.  
  144. // return the id of the savepanel
  145. - savePanel
  146. {
  147.     return savePanel;
  148. }
  149.  
  150. @end